import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaModule } from './modules/prisma/prisma.module';
import { UserModule } from './modules/user/user.module';
import { RedisModule } from './modules/redis/redis.module';
import { EmailModule } from './modules/email/email.module';
import { JwtModule } from '@nestjs/jwt';
import { ScheduleModule } from '@nestjs/schedule';
import { APP_FILTER, APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './auth.guard';
import { FriendshipModule } from './modules/friendship/friendship.module';
import { ChatroomModule } from './modules/chatroom/chatroom.module';
import { HttpExceptionFilter } from './filter/http-exception.filter';
import { TypeOrmExceptionFilter } from './filter/typeorm-exception.filter';
import { UnknowExceptionFilter } from './filter/unknow-exception.filter';
import { ConfigModule } from '@nestjs/config';
import * as path from 'path';
import { PrismaExceptionFilter } from './filter/prisma-exception.filter';
import { SchedulerService } from './service/scheduler.service';

const ENV = process.env.NODE_ENV || 'development';
const devEnvFilePath = 'env/.env.' + ENV;
console.log('devEnvFilePath:', devEnvFilePath);
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      expandVariables: true,
      envFilePath: [path.join(__dirname, devEnvFilePath)],
    }),
    ScheduleModule.forRoot({
      cronJobs: true,
    }),
    PrismaModule,
    UserModule,
    RedisModule,
    EmailModule,
    JwtModule.registerAsync({
      global: true,
      useFactory: () => ({
        secret: 'yqg',
        signOptions: {
          expiresIn: '30m',
        },
      }),
    }),
    FriendshipModule,
    ChatroomModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    SchedulerService,
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
    {
      provide: APP_FILTER,
      useClass: UnknowExceptionFilter,
    },
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },

    {
      provide: APP_FILTER,
      useClass: PrismaExceptionFilter,
    },
  ],
  exports: [SchedulerService],
})
export class AppModule {}
